<?php
/**
 * Created by PhpStorm.
 * User: hulinhua
 * Date: 2018/1/29
 * Time: 下午4:08
 */

namespace app\index\controller;

use app\index\model\LoginModel;
use think\controller;
use think\Request;


class LoginController extends controller
{
    public function index()
    {
        if (session('?userInfo')) {
            $this->success('您已经登录了，直接跳转到成功页！',url('/home'));
        }else{
            return $this->fetch('login');
        }
    }


    /**
     * @return bool
     */
    public function login_check(Request $request)
    {
        if ($request->request('Submit')=='注册'){
            return view('register');
        }
        else if ($request->request('Submit')=='登陆'){
            $this->loginButtonAction($request);
        }else{
            echo '忘记密码';
        }
    }

    protected function loginButtonAction($request){

        $userName = $request->request('username');
        $pw = $request->request('password');

        $loginModel = new LoginModel();
        $checkResult =  $loginModel->checkUser($userName,$pw);

        if ($checkResult['code']){
            $this->success($checkResult['msg'], url('/home'));
        }else{
            $this->error($checkResult['msg']);
        }

    }

    public function register()
    {
        return view('register');
    }

    public function signup_check(Request $request){

        $model = new LoginModel();
        $model->bindRequest($request);
        $model->checkRegister();

        if($model->register_success){
            $this->success('您已成功注册,即将返回到登陆界面',url('/login'));
        }else{
            $this->error($model->register_error);
        }

    }


}